/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.zjdt.epserver.db.dao4mysql;

import com.zjdt.epserver.db.dao.ProductDao;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;

/**
 *
 * @author dusin
 */
public class ProductDaoImpl implements ProductDao{

    static Logger logger = Logger.getLogger(ProductDaoImpl.class);
    private JdbcTemplate jdbcTemplate;

    public ProductDaoImpl(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }       
    
    @Override
    public boolean doCreate(Object[] bean) {
        boolean flag = true;
        String sql = null;
        sql = "insert into product(code,name,note,fcode) values(?,?,?,?)";
        logger.info(sql);
        int rows = jdbcTemplate.update(sql, bean);
        if (rows <= 0) {
            flag = false;
        }
        return flag;
    }

    @Override
    public boolean doDeleteById(int id) {
        boolean flag = true;
        String sql = null;
        sql = "delete from product where id=?";
        int rows = jdbcTemplate.update(sql, id);
        if (rows <= 0) {
            flag = false;
        }
        return flag;
    }

    @Override
    public boolean doUpdateById(Object[] bean) {
        boolean flag = true;
        String sql = null;
        sql = "update product set code=?,name=?,note=? where id=?";
        int rows = jdbcTemplate.update(sql, bean);
        if (rows <= 0) {
            flag = false;
        }
        return flag;
    }    

    @Override
    public Map<String, Object> findById(int id) {
        String sql = null;
        try{
            sql = "select * from  product where id = ?";
            return jdbcTemplate.queryForMap(sql,id);        
        }catch(EmptyResultDataAccessException e){
            return null;
        }        
    }

    @Override
    public List<Map<String, Object>> findAllPaper() {        
        String sql = "select * from product where code like '1%'";
        return jdbcTemplate.queryForList(sql);
    }
    @Override
    public List<Map<String, Object>> findAllSteel() {        
        String sql = "select * from product where code like '2%'";
        return jdbcTemplate.queryForList(sql);
    }

    @Override
    public List<Map<String, Object>> findAllPaperFuzzy(String key) {
        String sql = "select * from product where code like '1%' and (code like ? or name like ?)";
        return jdbcTemplate.queryForList(sql,"%"+key+"%","%"+key+"%");
    }

    @Override
    public List<Map<String, Object>> findAllSteelFuzzy(String key) {
        String sql = "select * from product where code like '2%' and (code like ? or name like ?)";
        return jdbcTemplate.queryForList(sql,"%"+key+"%","%"+key+"%");
    }

    @Override
    public List<Map<String, Object>> findAllByFactory(String factoryCode) {
        String sql = "select * from product where fcode = ?";
        return jdbcTemplate.queryForList(sql,factoryCode);
    }
    
}
